import Cookies from 'js-cookie'
import config from '@/config'

const {cookieExpires } = config

export const TOKEN_KEY = 'token'

export const setToken = (token) => {
  Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
}

export const getToken = () => {
  const token = Cookies.get(TOKEN_KEY)
  if (token) return token
  else return false
}

export function getUuid () {
  return new Date().getTime()
}

// 将数组指定位置的元素上移一位
export function arrayUpGo (array, index) {
  if (index !== 0) {
    array[index] = array.splice(index - 1, 1, array[index])[0]
  } else {
    array.push(array.shift())
  }
}

// 将数组指定位置的元素下移一位
export function arrayDownGo (array, index) {
  if (index !== array.length - 1) {
    array[index] = array.splice(index + 1, 1, array[index])[0]
  } else {
    array.unshift(array.splice(index, 1)[0])
  }
}
// 防抖
export function debounce(func, wait) {
  return function () {
    let context = this
    let args = arguments
    if (func.id) {
      clearTimeout(func.id)
    }
    func.id = setTimeout(() => {
      func.apply(context, args)
    }, wait)
  }
}
